pipeline {
    agent any

    stages {
//        stage("prepare") {
//            environment {
//                s3_access_key = credentials('swift-s3-access-key')
//                s3_secret_key = credentials('swift-s3-secret-key')
//            }
//            steps {
//                // create a new "directory" on the S3 storage for the current test outputs
//                sh """
//                bucket=dbcsr-artifacts
//                host=object.cscs.ch
//
//                resource="/\${bucket}/logs/build-${env.BUILD_NUMBER}/"
//                content_type="application/directory"
//                date=`date -R`
//                _signature="PUT\\n\\n\${content_type}\\n\${date}\\n\${resource}"
//                signature=`echo -en \${_signature} | openssl sha1 -hmac \${s3_secret_key} -binary | base64`
//
//                curl --fail -v -X PUT \
//                    -H "Host: \$host" \
//                    -H "Date: \${date}" \
//                    -H "Content-Length: 0" \
//                    -H "Content-Type: \${content_type}" \
//                    -H "Authorization: AWS \${s3_access_key}:\${signature}" \
//                    https://\$host\${resource}
//                """
//            }
//        }
        stage("build and test") {
            parallel {
                stage("CRAY") {
                    stages {
                        stage('build') {
                            steps {
                                run_batch("0:30:00", "cray", "build")
                            }
                        }
//                        stage('test') {
//                            steps {
//                                run_batch("1:00:00", "cray", "test")
//                            }
//                        }
                    }
                }
                stage("GNU") {
                    stages {
                        stage('build') {
                            steps {
                                run_batch("0:15:00", "gnu", "build")
                            }
                        }
                        stage('test') {
                            steps {
                                run_batch("1:00:00", "gnu", "test")
                            }
                        }
                    }
                }
                stage("OpenCL") {
                    stages {
                        stage('build') {
                            steps {
                                run_batch("0:15:00", "ocl", "build")
                            }
                        }
                        stage('test') {
                            steps {
                                run_batch("1:00:00", "ocl", "test")
                            }
                        }
                    }
                }
                stage("Intel") {
                    stages {
                        stage('build') {
                            steps {
                                run_batch("0:30:00", "intel", "build")
                            }
                        }
                        stage('test') {
                            steps {
                                run_batch("1:00:00", "intel", "test")
                            }
                        }
                    }
                }
            }
        }
    }
}

def run_batch(timelimit, environment, task) {
    def (account, basename) = env.JOB_NAME.split('/')
    def sbatch_script = ".ci/daint.cscs.ch/${environment}.${task}.sh"
    def sbatch_out = "sbatch.${env.BUILD_TAG}.${environment}.${task}.out"

    // avoid using the shell for variable expansion to
    // get the final command displayed in Jenkins
    try {
        sh """
        sbatch --wait \
            --time="${timelimit}" \
            --account="${account}" \
            --job-name="${basename}.${environment}.${task}" \
            --output="${sbatch_out}" \
            ${sbatch_script}
        """
    }
    finally {
        echo readFile("${sbatch_out}")

//        withCredentials([string(credentialsId: 'swift-s3-access-key', variable: 's3_access_key'), string(credentialsId: 'swift-s3-secret-key', variable: 's3_secret_key')]) {
//            sh """
//            bucket=dbcsr-artifacts
//            host=object.cscs.ch
//
//            resource="/\${bucket}/logs/build-${env.BUILD_NUMBER}/${environment}.${task}.out"
//            content_type="text/plain"
//            date=`date -R`
//            _signature="PUT\\n\\n\${content_type}\\n\${date}\\n\${resource}"
//            signature=`echo -en \${_signature} | openssl sha1 -hmac \${s3_secret_key} -binary | base64`
//
//            curl --fail -v -X PUT -T "${sbatch_out}" \
//                -H "Host: \$host" \
//                -H "Date: \${date}" \
//                -H "Content-Type: \${content_type}" \
//                -H "Authorization: AWS \${s3_access_key}:\${signature}" \
//                https://\$host\${resource}
//            """
//        }
    }
}

// vim: set filetype=groovy ts=4 sw=4 tw=0 :
